﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nature.Data;

namespace Nature.Toolkit.GMix
{
    public interface IExpression
    {
        IEnumerable<IComponent> Components { get; }
    }

    public static partial class Extensions
    {
        [DebuggerStepThrough()]
        public static IEnumerable<MixProjection> AsEnumerable(this IExpression expression, System.Collections.IEnumerable entitiesHeap)
        {
            return new MixProjectionCollection(expression, entitiesHeap);
        }

    }

    public static partial class Extensions
    {
        public static IEnumerable<string> GetSpeciesIDs(this IExpression expression)
        {
            return
                from c in expression.Components
                from lhs in c.LhsSet.OfType<ILhsSpecies>()
                select lhs.SpeciesId;
        }

        public static IEnumerable<IRhsEnumerable> GetRhsEnumerations(this IExpression expression)
        {
            return
                from c in expression.Components
                from l in c.LhsSet
                let r = c[l] as IRhsEnumerable
                where r != null
                select r;
        }

        public static IComponent FindComponent(this IExpression expression, string componentName)
        {
            return
                expression.Components.SingleOrDefault(c=>
                    StringComparer.InvariantCultureIgnoreCase.Compare(c.ComponentName, componentName) == 0
                );
        }
    }
}
